package org.zeroturnaround.javarebel.integration.jmonkey;

import java.lang.reflect.Method;

/**
 * Created by IntelliJ IDEA.
 * User: czarnik_a
 * Date: 14.02.11
 * Time: 10:40
 * To change this template use File | Settings | File Templates.
 */
public class JMonkeyAppConfigReloader {

    public static void initInstance(Object o) {
        Class baseClass = o.getClass();
        for (Method methode : baseClass.getMethods()) {
            if (methode.getName().equals("setInstance")) {
                try {
                    methode.setAccessible(true);
                    methode.invoke(o, new Object[]{o});
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void reinitialize(Class baseClass)
            throws Exception {
        for (Method methode : baseClass.getMethods()) {
            if (methode.getName().equals("getInstance")) {
                methode.setAccessible(true);
                Object instance = methode.invoke(baseClass.newInstance(), new Object[0]);
                Method restartallMethod = baseClass.getMethod("restartInstance", new Class[0]);
                restartallMethod.setAccessible(true);
                restartallMethod.invoke(instance, new Object[0]);
            }
        }
    }

}
